package com.domainlanguage.time;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.util.EqualsHelper;

import java.io.Serializable;

/**
 * Base class for immutable composite types.
 * 
 */
public abstract class ImmutableCompositeUserType implements CompositeUserType {

    @Override
    public final boolean equals(final Object x, final Object y) throws HibernateException {
        return EqualsHelper.equals(x, y);
    }

    @Override
    public final int hashCode(final Object x) throws HibernateException {
        return x == null ? 0 : x.hashCode();
    }

    @Override
    public final Object deepCopy(final Object value) throws HibernateException {
        return value;
    }

    @Override
    public final boolean isMutable() {
        return false;
    }

    @Override
    public final Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
        return (Serializable) value;
    }

    @Override
    public final Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
        return cached;
    }

    @Override
    public final Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
        return original;
    }

    @Override
    public final void setPropertyValue(Object component, int property, Object value) throws HibernateException {
        // Immutable
    }
}
